/**
 * 
 */
package test.dao;

import static org.junit.Assert.fail;
import hibernate.dao.impl.BaseDAO;
import hibernate.pojo.Users;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * @author zhangzhw
 * 
 */
public class testBaseDAO {

	ApplicationContext ctx;
	BaseDAO bd;

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		ctx = new FileSystemXmlApplicationContext(
				"/WebRoot/WEB-INF/applicationContext.xml");
		bd = (BaseDAO) ctx.getBean("baseDAO");
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		bd=null;
		ctx=null;
	}

	/**
	 * Test method for
	 * {@link hibernate.dao.impl.BaseDAO#delete(java.lang.Object)}.
	 */
	@Test
	public final void testDelete() {
		Users user=new Users();
		user.setUsername("lee");
		bd.save(user);
		bd.delete(user);
	}

	/**
	 * Test method for
	 * {@link hibernate.dao.impl.BaseDAO#findAll(java.lang.String)}.
	 */
	@Test
	public final void testFindAllString() {
		// System.out.println("test find all");
		// BaseDAOI bd=new BaseDAO();
		// List list=bd.findAll(" from Users ");
		// //end
		bd.findAll(" from Users ");

	}

	/**
	 * Test method for
	 * {@link hibernate.dao.impl.BaseDAO#findByComplex(java.lang.String, java.lang.String[], java.lang.Object[])}.
	 */
	@Test
	public final void testFindByComplex() {
		
	}

	/**
	 * Test method for
	 * {@link hibernate.dao.impl.BaseDAO#findByExample(java.lang.Object)}.
	 */
	@Test
	public final void testFindByExample() {
		Users user=new Users();
		user.setUsername("wang");
		bd.findByExample(user);
	}

	/**
	 * Test method for
	 * {@link hibernate.dao.impl.BaseDAO#findByID(java.lang.String, java.lang.Long)}.
	 */
	@Test
	public final void testFindByID() {
		bd.findByID(" Users ", 2L);
	}

	/**
	 * Test method for
	 * {@link hibernate.dao.impl.BaseDAO#findByProperty(java.lang.String, java.lang.String, java.lang.Object)}.
	 */
	@Test
	public final void testFindByProperty() {
		bd.findByProperty("Users", "username", "zhang");
	}

	/**
	 * Test method for
	 * {@link hibernate.dao.impl.BaseDAO#merge(java.lang.Object)}.
	 */
	@Test
	public final void testMerge() {
		Users u=new Users();
		u.setUsername("zhang");
		
		List list=bd.findByProperty("Users", "username", "zhang");
		Users user=(Users)list.get(0);
		user.setUserpass("passwd");
		bd.merge(user);
		
		bd.delete(user);
	}

	/**
	 * Test method for {@link hibernate.dao.impl.BaseDAO#save(java.lang.Object)}.
	 */
	@Test
	public final void testSave() {
		Users user = new Users();
		user.setUsername("zhang");
		user.setMemo("this is a test");
		bd.save(user);
	}

	/**
	 * Test method for
	 * {@link hibernate.dao.impl.BaseDAO#uporsave(java.lang.Object)}.
	 */
	@Test
	public final void testUporsave() {
		Users u=new Users();
		u.setUsername("zhang");
		
		List list=bd.findByProperty("Users", "username", "zhang");
		Users user=(Users)list.get(0);
		user.setUserpass("passwd");
		bd.uporsave(user);
		
		Users user1=new Users();
		user1.setUsername("wang");
		bd.uporsave(user1);
		
		bd.delete(user);
		bd.delete(user1);
	}

	/**
	 * Test method for
	 * {@link hibernate.dao.impl.BaseDAO#update(java.lang.Object)}.
	 */
	@Test
	public final void testUpdate() {
		Users u=new Users();
		u.setUsername("zhang");
		
		List list=bd.findByProperty("Users", "username", "zhang");
		Users user=(Users)list.get(0);
		user.setUserpass("passwd");
		bd.update(user);
		
		bd.delete(user);
	}

	/**
	 * Test method for
	 * {@link hibernate.dao.impl.BaseDAO#findAll(java.lang.String, int, int)}.
	 */
	@Test
	public final void testFindAllStringIntInt() {
		bd.findAll("from Users" , 1, 5);
	}

	/**
	 * Test method for
	 * {@link hibernate.dao.impl.BaseDAO#findRec(java.lang.String, int, int)}.
	 */
	@Test
	public final void testFindRec() {
		bd.findRec(" from Users ", 1, 5);
	}

	/**
	 * Test method for
	 * {@link hibernate.dao.impl.BaseDAO#getCount(java.lang.String)}.
	 */
	@Test
	public final void testGetCount() {
		bd.getCount(" from Users");
	}

}
